package com.hq.petmap;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hq.petmap.model.Pet;
import com.hq.petmap.service.BackgroundService;

public class NearbyListAdapter extends BaseAdapter {
	private final Context context;
	private List<Pet> petsList;
	private final BackgroundService backgroundService;

	public NearbyListAdapter(Context context, List<Pet> values) {
		this.context = context;
		this.petsList = values;
		this.backgroundService = new BackgroundService(context);
	}
	

    public List<Pet> getPetsList() {
		return petsList;
	}
    
    public void addPetsList(List<Pet> petsList) {
    	this.petsList.addAll(petsList);
	}

	public void setPetsList(List<Pet> petsList) {
		this.petsList = petsList;
	}


	public int getCount() {
        return petsList.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
    	View rowView;
    	if(position >= getCount()) {
    		rowView = convertView;    		
    	} else {
    		LayoutInflater inflater = (LayoutInflater) context
    				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    		rowView = inflater.inflate(R.layout.nearby_item, parent, false);
            TextView title = (TextView)rowView.findViewById(R.id.title);
            TextView artist = (TextView)rowView.findViewById(R.id.artist);
            TextView duration = (TextView)rowView.findViewById(R.id.duration);
            ImageView face=(ImageView)rowView.findViewById(R.id.list_image);
            
            Pet pet = petsList.get(position);
            
            title.setText(pet.getName());
            artist.setText(pet.getUrl());
            duration.setText("4.00");
            Bitmap bitmap = backgroundService.GetImage(pet.getUrl(), face);
            if(bitmap != null) {
            	face.setImageBitmap(bitmap);        	
            } else {
            	face.setImageResource(R.drawable.ios_logo);
            }    		
    	}
		return rowView;
    }
	
}
